package ak;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.dispatch.OnComplete;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.Await;
import scala.concurrent.Awaitable;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

public class Service {

	ActorSystem aAystem;

	public Service() {
		aAystem = ActorSystem.create("001");
	}

	public void start() throws Exception {
		ActorRef actor = aAystem.actorOf(Props.create(Boss.class));
		Timeout timeOut = new Timeout(Duration.create(1800, "seconds"));
		
		Future<Object> future1 = Patterns.ask(actor, "789", timeOut);
		Object obj = Await.result(future1, timeOut.duration());
		System.out.println("onComplete1=" + obj);
		
		
		Future<Object> future = Patterns.ask(actor, "123", timeOut);
		future.onComplete(new OnComplete<Object>() {

			@Override
			public void onComplete(Throwable arg0, Object arg1) throws Throwable {
				// actor.tell(PoisonPill.getInstance() , actor);
				System.out.println("onComplete=" + arg1);
			}

		}, aAystem.dispatcher());

//		ActorRef actor1 = aAystem.actorOf(Props.create(Boss.class));
//		Future<Object> future1 = Patterns.ask(actor, "789", timeOut);
//		Object obj = Await.result(future1, timeOut.duration());
//		System.out.println("onComplete1=" + obj);

//		actor.tell(PoisonPill.getInstance(), ActorRef.noSender());
//		aAystem.shutdown();
//		System.out.println("shutdown");
	}

	public static void main(String[] args) throws Exception {
		new Service().start();
	}
}
